package cn.powerone.scm.service.impl;

import java.util.Date;
import java.util.List;
import cn.powerone.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmProjectsMapper;
import cn.powerone.scm.domain.ScmProjects;
import cn.powerone.scm.service.IScmProjectsService;

/**
 * 项目管理Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-06-23
 */
@Service
public class ScmProjectsServiceImpl implements IScmProjectsService 
{
    @Autowired
    private ScmProjectsMapper scmProjectsMapper;

    /**
     * 查询项目管理
     * 
     * @param projectId 项目管理主键
     * @return 项目管理
     */
    @Override
    public ScmProjects selectScmProjectsByProjectId(Long projectId)
    {
        return scmProjectsMapper.selectScmProjectsByProjectId(projectId);
    }

    /**
     * 查询项目管理列表
     * 
     * @param scmProjects 项目管理
     * @return 项目管理
     */
    @Override
    public List<ScmProjects> selectScmProjectsList(ScmProjects scmProjects)
    {
        return scmProjectsMapper.selectScmProjectsList(scmProjects);
    }

    /**
     * 新增项目管理
     * 
     * @param scmProjects 项目管理
     * @return 结果
     */
    @Override
    public int insertScmProjects(ScmProjects scmProjects)
    {
        scmProjects.setCreateTime(DateUtils.getNowDate());
        return scmProjectsMapper.insertScmProjects(scmProjects);
    }

    /**
     * 修改项目管理
     * 
     * @param scmProjects 项目管理
     * @return 结果
     */
    @Override
    public int updateScmProjects(ScmProjects scmProjects)
    {
        scmProjects.setUpdateTime(DateUtils.getNowDate());
        return scmProjectsMapper.updateScmProjects(scmProjects);
    }

    /**
     * 批量删除项目管理
     * 
     * @param projectIds 需要删除的项目管理主键
     * @return 结果
     */
    @Override
    public int deleteScmProjectsByProjectIds(Long[] projectIds)
    {
        try {
            return scmProjectsMapper.deleteScmProjectsByProjectIds(projectIds);
        } catch (org.springframework.dao.DataIntegrityViolationException e) {
            // 外键约束违反，无法删除有关联数据的记录
            System.err.println("删除失败：存在关联数据，无法删除");
            throw new RuntimeException("删除失败：存在关联数据，无法删除");
        } catch (Exception e) {
            System.err.println("删除过程中发生错误: " + e.getMessage());
            throw new RuntimeException("删除失败：" + e.getMessage());
        }
    }

    /**
     * 删除项目管理信息
     * 
     * @param projectId 项目管理主键
     * @return 结果
     */
    @Override
    public int deleteScmProjectsByProjectId(Long projectId)
    {
        return scmProjectsMapper.deleteScmProjectsByProjectId(projectId);
    }

    @Override
    public int selectProjectsCountByMonth(Date yesterday) {
        return scmProjectsMapper.selectProjectsCountByMonth(yesterday);
    }

    @Override
    public int selectProjectsCountByYear(Date yesterday) {
        return scmProjectsMapper.selectProjectsCountByYear(yesterday);
    }

    @Override
    public int selectProjectsCountAll() {
        return scmProjectsMapper.selectProjectsCountAll();
    }
}
